package com.xl.bigdata.manager;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.client.coprocessor.AggregationClient;
import org.apache.hadoop.hbase.client.coprocessor.LongColumnInterpreter;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @ClassName HbaseClient
 * @Description TODO
 * @Author xl.gao
 * @Date 2022/3/18 17:14
 * @Version
 */
public class HbaseClient {

    private volatile static Connection conn = null;

    /**
     * 获得链接
     */
    public static Connection getConnection(String zk) {

        try {
            if(conn == null || conn.isClosed()){
                synchronized(HbaseClient.class){
                    if(conn == null || conn.isClosed()){
                        Configuration conf = HBaseConfiguration.create();
                        conf.set("hbase.zookeeper.quorum", zk);
                        conf.set("zookeeper.znode.parent", "/hbase");
                        conn = ConnectionFactory.createConnection(conf);
                        System.out.println("HbaseClient - 获取hbase连接成功...");
                    } else {
                        System.out.println("HbaseClient - 重复使用hbase连接...双重检查最内层...");
                    }
                }
            } else {
                System.out.println("HbaseClient - 重复使用hbase连接...双重检查最外层...");
            }
        } catch (IOException e) {

            if (conn != null) {
                try {
                    conn.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            System.out.println("HbaseClient - 获取hbase连接失败...");
            e.printStackTrace();
        }

        return conn;

    }

    public static void main(String[] args) {

        String tableName = "CMR";
        String zk = "lx-es-08,lx-es-09,lx-es-10";

        long start = 1647578613000L;
        long end = 1647596613000L;

        Connection connection = HbaseClient.getConnection(zk);

        try {
            Table table = connection.getTable(TableName.valueOf(tableName));

            Scan scan = new Scan();
            scan.setTimeRange(start, end);

            ResultScanner scanner = table.getScanner(scan);
            scanner.forEach( x ->{
                System.out.println(Bytes.toString(x.getRow()));;
            });

//            System.out.println("总计 ：" + scanner.);

            scanner.close();

            table.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                connection.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
